package insert10000;

/**
 * @author 况祥彬
 * @date 2020/12/20 11:02
 **/

import java.util.Random;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class query {
    public static void main(String[] args) {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/testdb";
        String user = "root";
        String password = "123456";
        try {
            // 加载驱动程序
            Class.forName(driver);
            // 连续数据库
            Connection conn = DriverManager.getConnection(url, user, password);
            if (!conn.isClosed())
                System.out.println("Succeeded connecting to the Database!");
            // statement用来执行SQL语句
            Statement statement = conn.createStatement();
            String sqll = null;
            for (int key2 = 0; key2 <= 10000; key2++) {
                String key1 = randomString(3);
//丢了引号会出错，找不到collumn“ID”或者“rdname”int型的可以不用加，但是string类型的必须加。
                sqll = "insert into single_table  (key1,key2) values ('" + key1 + "','"+key2+"')";
                statement.execute(sqll);
            }
            statement.close();
            conn.close();

        } catch (ClassNotFoundException e) {

            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();

        } catch (SQLException e) {

            e.printStackTrace();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    /**
     * 产生随机字符串
     */
    private static Random randGen = null;
    private static char[] letters = null;

    public static final String randomString(int length) {
        if (length < 1) {
            return null;
        }
        if (randGen == null) {
            randGen = new Random();
            letters = ("abcdefghij").toCharArray();
        }
        char[] randBuffer = new char[length];
        for (int i = 0; i < randBuffer.length; i++) {
            randBuffer[i] = letters[randGen.nextInt(10)];
        }
        return new String(randBuffer);
    }

}
